﻿using AutoMapper;
using Newtonsoft.Json;
using Fortunate.API.BASE;
using Fortunate.IService;
using Fortunate.ViewModel;
using Fortunate.Common.Extensions;
using Fortunate.Common.Helper;
using Microsoft.AspNetCore.Mvc;
using Fortunate.ViewModel.UserModule;

namespace Fortunate.API.Controllers
{
    /// <summary>
    /// 用户
    /// </summary>
    [Route("api/[controller]/[action]")]
    public class UsersController : BaseApiController
    {
        /// <summary>
        /// 
        /// </summary>
        protected readonly IUsersService _Users;
        /// <summary>
        /// 
        /// </summary>
        protected readonly ILoginService _Login;
        /// <summary>
        /// 
        /// </summary>
        protected readonly IMapper _mapper;

        private string GetSalt => PasswordHelper.GetPasswordSalt();
        /// <summary>
        /// 
        /// </summary>
        public UsersController(IUsersService Users, IMapper mapper, ILoginService Login)
        {
            _Users = Users;
            _mapper = mapper;
            _Login = Login;
        }

        /// <summary>
        /// 获取用户信息列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetUsersList([FromQuery] UsersSearch search)
        {
            search.PageIndex = search.PageIndex <= 0 ? 1 : search.PageIndex;
            search.PageCount = search.PageCount <= 0 ? 20 : search.PageCount;
            var objModel = await _Users.GetUsersListAsync(search);
            DataModel<UsersList> dataModel = new DataModel<UsersList>()
            {
                Total = objModel.Total,
                Data = _mapper.Map<List<UsersList>>(objModel.Data)
            };
            var data = JsonConvert.DeserializeObject<DataModel<UsersList>>(JsonConvert.SerializeObject(dataModel));
            return Success(data);
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetUsersObject(string id)
        {
            var entity = await _Users.GetUsersObjectAsync(id.ToGuid());
            entity.Password = "";
            return Success(entity);
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> PostUsers([FromBody] UsersModel users)
        {
            users.Id = GetGuid;
            users.Salt = GetSalt;
            users.Password = PasswordHelper.GenEncodingPassword(users.Password, users.Salt);
            users.CreateTime = DateTime.Now;
            users.UserState = UserState.正常;

            var entity = _mapper.Map<Fortunate.Application.Entities.Users>(users);
            if (await _Users.InsertUsersAsync(entity))
                return Success("添加成功");
            else
                return Fail("添加异常");
        }

        /// <summary>
        /// 编辑用户信息
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> PutUsers([FromBody] UsersModel users)
        {
            users.Salt = GetSalt;
            users.Password = PasswordHelper.GenEncodingPassword(users.Password, users.Salt);

            if (await _Users.UpdateUsersAsync(_mapper.Map<Fortunate.Application.Entities.Users>(users)))
                return Success("修改成功");
            else
                return Fail("修改异常");
        }

        /// <summary>
        /// 冻结账号
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DeleteUsers([FromBody] UserInput input)
        {
            if (await _Users.DeleteUsersAsync(input))
                return Success("冻结成功");
            else
                return Fail("冻结异常");
        }

        /// <summary>
        /// 获取日历
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetHolidays(string year)
        {
            //需要返回的数据
            List<HolidaysModel> Holidays = new List<HolidaysModel>();
            //月份
            for (int mon = 1; mon <= 12; mon++)
            {
                //拼接 年-月  以 - 为区分
                string month = (mon <= 9 ? "0" + mon : mon.ToString());
                string YearMonth = year + "-" + month;
                //天
                for (int day = 1; day <= MonthDays(Convert.ToInt32(year), mon); day++)
                {
                    HolidaysModel holidaysModel = new HolidaysModel();

                    string dayStr = day <= 9 ? ("0" + day) : day.ToString();
                    if (day <= 9)
                    {
                        holidaysModel.holidayDate = YearMonth + "-0" + day;
                    }
                    else
                    {
                        holidaysModel.holidayDate = YearMonth + "-" + day;
                    }
                    holidaysModel.Week = Convert.ToDateTime(YearMonth + "-" + day).DayOfWeek.ToString();
                    Holidays.Add(holidaysModel);
                }
            }
            return Success(Holidays);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class HolidaysModel
    {
        public string holidayDate { get; set; }
        public int holidayFlag { get; set; } = 1;
        public string Week { get; set; }
    }
}
